//
//  MainView.m
//  playSerengetiMobile1
//
//  Created by Lita Gratrix on 10/27/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MainView.h"
#import "LogonView.h"
#import "UserHome.h"
#import "UserCentral.h"
#import "TeamCentral.h"
#import "LocationCentral.h"

#define USER_HOME_VIEW_ELEMENTS			7
#define USER_CENTRAL_VIEW_ELEMENTS		2
#define TEAM_CENTRAL_VIEW_ELEMENTS		2
#define LOCATION_CENTRAL_VIEW_ELEMENTS	3

@implementation MainView

@synthesize mainTabBar;
@synthesize appDelegate;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:@"MainView" bundle:nil]) {
		self.appDelegate = (playSerengetiMobile1AppDelegate *)[[UIApplication sharedApplication] delegate];
//		self.view = mainTabBar.view;

    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.appDelegate = (playSerengetiMobile1AppDelegate *)[[UIApplication sharedApplication] delegate];
	self.view = mainTabBar.view;
	mainTabBar.delegate = self;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	[appDelegate release];
	[mainTabBar release];
    [super dealloc];
}

#pragma mark -
#pragma mark UITabBarControllerDelegate methods

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//	NSLog(@"entering tabBarController\n");
	UIView *lastView = [viewController.view.subviews lastObject];
	NSInteger elementCount = [viewController.view.subviews count];
	BOOL userHomeCheck = [viewController isKindOfClass:[UserHome class]] && elementCount > USER_HOME_VIEW_ELEMENTS;
	BOOL userCentralCheck = [viewController isKindOfClass:[UserCentral class]] && elementCount > USER_CENTRAL_VIEW_ELEMENTS;
	BOOL teamCentralCheck = [viewController isKindOfClass:[TeamCentral class]] && elementCount > TEAM_CENTRAL_VIEW_ELEMENTS;
	BOOL locationCentralCheck = [viewController isKindOfClass:[LocationCentral class]] && elementCount > LOCATION_CENTRAL_VIEW_ELEMENTS;
	NSLog(@"viewController.view.subviews for %@ has %d elements", [viewController class], [viewController.view.subviews count]);
	if(userHomeCheck || userCentralCheck || teamCentralCheck || locationCentralCheck){
		[lastView removeFromSuperview];
	}
}	

@end
